package com.ms.game.slot.base;

import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.ms.service.error.ErrorCode;
import com.xpec.c4.db.model.basic.BasicGameModel;
import com.xpec.c4.game.utility.GUID;

public class DMSeatInfoManager<T>
{
	@SuppressWarnings("unused")
	private static final Log log = LogFactory.getLog(DMSeatInfoManager.class);
	
	
	@SuppressWarnings("rawtypes")
	private ConcurrentHashMap<String , DMBasicSeatInfoDatas<T>> hallSeatData = new ConcurrentHashMap<String, DMBasicSeatInfoDatas<T>>();
	
	private ConcurrentHashMap<String,DMBasicSeatInfo>  seatUser = new ConcurrentHashMap<String,DMBasicSeatInfo>();
	
	//暫時屏蔽玩家重覆請求入座 guid / seatData
	private ConcurrentHashMap<String, DMBasicSeatInfo> tmpblockUser = new ConcurrentHashMap<String, DMBasicSeatInfo>();
	
	/** 館 / sessions */
	private ConcurrentHashMap<String, ConcurrentHashMap<String, GUID>> hallSeatSessions = new ConcurrentHashMap<String, ConcurrentHashMap<String, GUID>>();

	public ConcurrentHashMap<String, ConcurrentHashMap<String,GUID>> getHallSessions() {
		return hallSeatSessions;
	}
	
	//guid是否被暫時屏蔽?
	public boolean isTmpBlockUser(String guid)
	{
		if(!tmpblockUser.containsKey(guid))
		{
			return false;
		}
		
		return true;
	}
	
	//取出被屏蔽腳色的座位資訊
	public DMBasicSeatInfo getTempBlockInfo(String guid) {
		DMBasicSeatInfo seat = null;
		if (tmpblockUser.containsKey(guid)) {
			seat = tmpblockUser.get(guid);
		}
		return seat;
	}
	
	//guid加入暫時屏蔽清單
	public void addTmpBlockUser(String guid, DMBasicSeatInfo seat)
	{
		tmpblockUser.put(guid, seat);
	}
	
	//guid移除被暫時屏蔽清單
	public void delTmpBlockUser(String guid)
	{
		if(tmpblockUser.containsKey(guid))
		{
			tmpblockUser.remove(guid);
		}
	}
	
	@SuppressWarnings("rawtypes")
	public ConcurrentHashMap<String , DMBasicSeatInfoDatas<T>> getHallSeatData() {
		return hallSeatData;
	}

	@SuppressWarnings("rawtypes")
	public void setHallSeatData(ConcurrentHashMap<String, DMBasicSeatInfoDatas<T>> hallSeatData) 
	{
		for(Entry<String,DMBasicSeatInfoDatas<T>> entry:hallSeatData.entrySet())
		{
			if(!(entry.getValue() instanceof DMBasicSeatInfoDatas))
			{	
				printError();
				return;
			}
		}
		
		this.hallSeatData = hallSeatData;
	}
	
	/**
	 * 新增館別資料容器
	 * @param halltype
	 * @param halldata
	 */
	public void addHallSeatdata(String halltype, DMBasicSeatInfoDatas<T> halldata)
	{
		hallSeatData.put(halltype, halldata);
	}
	
	/**
	 * 讀取館別座位資料
	 * @param halltype
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public DMBasicSeatInfoDatas<T> getHallSeatData(String halltype)
	{
		if(!hallSeatData.containsKey(halltype))
		{
			return null;
		}
		
		return hallSeatData.get(halltype);
	}
	
	/**
	 * 讀取空座位
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public  DMBasicSeatInfo getEmpty(String halltype, int gainRateID)
	{
		if(!hallSeatData.containsKey(halltype))
		{
			return null;
		}
		
		DMBasicSeatInfoDatas t = hallSeatData.get(halltype);
		return (DMBasicSeatInfo) ((DMBasicSeatInfoDatas)t).randEmptySeat(gainRateID);
	}
	
	
	@SuppressWarnings("rawtypes")
	public DMBasicSeatInfo getSeatData(String halltype , String seatid)
	{
		if(!hallSeatData.containsKey(halltype))
		{
			return null;
		}
		
		DMBasicSeatInfoDatas t = hallSeatData.get(halltype);
		return  (DMBasicSeatInfo) ((DMBasicSeatInfoDatas)t).get(seatid);
	}
	
	
//	@SuppressWarnings("rawtypes")
//	public DMBasicSeatInfo getSeatDataByID(String halltype , int machineid)
//	{
//		if(!hallSeatData.containsKey(halltype))
//		{
//			return null;
//		}
//		
//		DMBasicSeatInfoDatas t = hallSeatData.get(halltype);
//		return  (DMBasicSeatInfo) ((DMBasicSeatInfoDatas)t).getByID(machineid);
//	}
	
	/**
	 * 座位是否在驗證中
	 * @param halltype
	 * @param seatid
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public boolean isVerify(String halltype , String seatid)
	{
		if(!hallSeatData.containsKey(halltype))
		{
			return true;
		}
		
		DMBasicSeatInfoDatas t = hallSeatData.get(halltype);
		DMBasicSeatInfo seat =  (DMBasicSeatInfo) ((DMBasicSeatInfoDatas)t).get(seatid);
		return seat.isVerify;
	}
	
	/**
	 * 取消座位驗證狀態
	 * @param halltype
	 * @param seatid
	 */
	@SuppressWarnings("rawtypes")
	public void cancelVerify(String halltype , String seatid)
	{
		if(!hallSeatData.containsKey(halltype))
		{
			return;
		}
		
		DMBasicSeatInfoDatas t = hallSeatData.get(halltype);
		DMBasicSeatInfo seat =  (DMBasicSeatInfo) ((DMBasicSeatInfoDatas)t).get(seatid);
		seat.isVerify=false;
	}
	
	
	/**
	 * 是否己入座
	 * @param guid
	 * @return
	 */
	public int isExistsSeat(String seatid,String guid)
	{
		if(seatUser.containsKey(guid))
		{
			DMBasicSeatInfo seat =seatUser.get(guid);
			if(seat.seatid.equals(seatid))
			{
				return ErrorCode.SUCCESS;
			}
			
			return ErrorCode.SEAT_HAS_PEOPLE;
		}
		
		return ErrorCode.SUCCESS;
	}
	
	public void addUserDoSeat(String guid , T seatdata)
	{
		seatUser.put(guid, (DMBasicSeatInfo) seatdata);
	}
	
	/**
	 * 此玩家是否己入座
	 * @param guid
	 * @return
	 */
	public boolean isUserDoSeat(String guid)
	{
		if(!seatUser.containsKey(guid))
		{
			return false;
		}
		
		return true;
	}
	
	
	/**
	 * 清除入座資料
	 * @param seatid
	 * @param guid
	 */
	public void delSeatUserData(String seatid,String guid)
	{
		if(seatUser.containsKey(guid))
		{
			seatUser.remove(guid);
		}
	}
	
	/**
	 * 是否可以入座
	 * @param el
	 * @param guid
	 * @return
	 */
	public int  isCanEnterSeat(T el , String  guid)
	{
		DMBasicSeatInfo seat = (DMBasicSeatInfo) el;
		
		if(
				seat.stat == BasicGameModel.PEOPLE
				&&
				!seat.guid.equals(guid)
		)
		{
			return ErrorCode.SEAT_HAS_PEOPLE;
		}
		
		
		if(
				(seat.stat==BasicGameModel.OFFLINE_KEEP ||
				seat.stat==BasicGameModel.KEEP || 
				seat.stat==BasicGameModel.OFFLINE_KEEP_SPIN)
				&&
				!seat.guid.equals(guid)
		)
		{
			return ErrorCode.SEAT_HAS_KEEP;
		}
		
		return ErrorCode.SUCCESS;
	}
	
	
	/**
	 * 增加館的連線紀錄
	 * @param halltype
	 * @param halldata
	 */
	public void addHallSessions(String hallType, String seatId, GUID sessionId) {
		ConcurrentHashMap<String, GUID> sessions = new ConcurrentHashMap<String, GUID>();
		if (hallSeatSessions.containsKey(hallType)) {
			sessions = hallSeatSessions.get(hallType);
		} else { // 如果沒有這個管
			// 加進去
			sessions = new ConcurrentHashMap<String, GUID>();
			hallSeatSessions.put(hallType, sessions);

		}
		sessions.put(seatId,sessionId);
	}
	
	/**
	 * 移除館的連線紀錄
	 * @param halltype
	 * @param halldata
	 */
	public void removeHallSessions(String hallType, String seatId) {
		ConcurrentHashMap<String, GUID> seatSessions = hallSeatSessions.get(hallType);
		if (seatSessions == null || seatSessions.size() < 0) {
			return;
		}

		seatSessions.remove(seatId);
	}
	
	/**
	 * 打印出不正確資料
	 * @return
	 */
	private boolean printError()
	{
		if(log.isErrorEnabled())
		{
			log.error("DMSeatInfoContainer Child Must be Instanceof [ com.ms.game.slot.base.DMBasicSeatInfoData<T>] !! Now instanceof is False");
		}
		
		return false;
	}
}
